package com.example.victory.service;

import com.alibaba.fastjson.JSONObject;
import com.example.victory.config.ServiceError;
import com.example.victory.swagger.handler.AppCustomerOverallBigdataApiDelegate;
import com.example.victory.swagger.handler.AppCustomerOverallLabelApiDelegate;
import com.example.victory.swagger.model.*;
import com.example.victory.utils.JsonUtils;
import com.cgws.fintech.sdk.api.inner.crmapi.model.*;
import com.cgws.fintech.sdk.api.inner.security.model.User;
import com.cgws.fintech.sdk.common.exception.GlobalException;
import com.example.victory.common.web.ServiceHelper;
import com.cgws.fintech.sdk.inner.utils.web.SessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class AppCustomerOverAllBigdata implements AppCustomerOverallBigdataApiDelegate {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private InnerOverAllBigDataService innerOverAllBigDataService;



    private String getCustomerId(ApiRequestLabelHand param){
        ServiceHelper.checkDataNotNull(param.getData(),"param.data");
        ServiceHelper.checkDataNotNull(param.getData().getCustomerId(),"param.data.customerId");
        return  param.getData().getCustomerId();
    }

    private String getCustomerAccount(){
        User user = sessionService.getCurrentUserSession(true).getUser();
        String crmId = user.getCrmId();
        if (StringUtils.isEmpty(crmId)) {
            throw new GlobalException(ServiceError.ACCOUNT_NOT_EXISTS);
        }
        return crmId;
    }

    @Override
    public ResponseEntity<ApiResponseAccountList> getAccountListOfCustomer(ApiRequestCustomerId param) {
        ServiceHelper.checkDataNotNull(param.getData(), "data");
        String customerId = param.getData().getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        ResponseEntity<ApiResponseInnerCrmapiStr> apiResponseInnerCrmapiStrResponseEntity = innerOverAllBigDataService.getAccountListOfCustomer(new ApiRequestInnerCrmapiStr().data(new ApiRequestInnerCrmapiStrData().params(jsonObject.toJSONString())));
        ApiResponseAccountListData resultEntity = JsonUtils.getResultEntityCommon(apiResponseInnerCrmapiStrResponseEntity, ApiResponseAccountListData.class);
        return ResponseEntity.ok(new ApiResponseAccountList().data(resultEntity));
    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeTodayEntrust> getCustomerOverAllTradeBigDataCreditTodayBargai(ApiRequestCustomerIdPage param) {
        ApiRequestCustomerIdPageData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        jsonObject.put("pageCount", data.getPageCount());
        jsonObject.put("pagePos", data.getPagePos());
        jsonObject.put("pageNum", data.getPageNum());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeBigDataCreditTodayBargai(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeTodayEntrustData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeTodayEntrustData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeTodayEntrust().data(resultEntity));

    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTodayEntrust> getCustomerOverAllTradeBigDataCreditTodayTrust(ApiRequestCustomerIdPage param) {
        ApiRequestCustomerIdPageData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        jsonObject.put("pageCount", data.getPageCount());
        jsonObject.put("pagePos", data.getPagePos());
        jsonObject.put("pageNum", data.getPageNum());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeBigDataCreditTodayTrust(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTodayEntrustData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTodayEntrustData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTodayEntrust().data(resultEntity));

    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeNormalBigdata> getCustomerOverAllTradeCreditBigdata(ApiRequestCustomerAccount param) {
        ApiRequestCustomerAccountData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeCreditBigdata(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeNormalBigdataData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeNormalBigdataData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeNormalBigdata().data(resultEntity));
    }



    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeNormalBigdata> getCustomerOverAllTradeNormalBigdata(ApiRequestCustomerAccount param) {
        ApiRequestCustomerAccountData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        jsonObject.put("appPlatform", data.getAppPlatform());
        jsonObject.put("appVerName", data.getAppVerName());

        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeNormalBigdata(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeNormalBigdataData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeNormalBigdataData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeNormalBigdata().data(resultEntity));
    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeOptionBigdata> getCustomerOverAllTradeOptionBigdata(ApiRequestCustomerAccount param) {
        ApiRequestCustomerAccountData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeOptionBigdata(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeOptionBigdataData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeOptionBigdataData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeOptionBigdata().data(resultEntity));
    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeOtcBigdata> getCustomerOverAllTradeOtcBigdata(ApiRequestCustomerAccount param) {
        ApiRequestCustomerAccountData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeOtcBigdata(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeOtcBigdataData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeOtcBigdataData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeOtcBigdata().data(resultEntity));
    }


    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTradeTodayEntrust> getCustomerOverAllTradeTodayBargain(ApiRequestCustomerIdPage param) {
        ApiRequestCustomerIdPageData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        jsonObject.put("pageCount", data.getPageCount());
        jsonObject.put("pagePos", data.getPagePos());
        jsonObject.put("pageNum", data.getPageNum());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeTodayBargain(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTradeTodayEntrustData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTradeTodayEntrustData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTradeTodayEntrust().data(resultEntity));
    }

    @Override
    public ResponseEntity<ApiResponseCustomerOverAllTodayEntrust> getCustomerOverAllTradeTodayEntrust(ApiRequestCustomerIdPage param) {
        ApiRequestCustomerIdPageData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        jsonObject.put("pageCount", data.getPageCount());
        jsonObject.put("pagePos", data.getPagePos());
        jsonObject.put("pageNum", data.getPageNum());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverAllTradeTodayEntrust(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerOverAllTodayEntrustData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerOverAllTodayEntrustData.class);
        return ResponseEntity.ok(new ApiResponseCustomerOverAllTodayEntrust().data(resultEntity));


    }

    @Override
    public ResponseEntity<ApiResponseGetCustomerOverallAsset> getCustomerOverallBigdataAsset(ApiRequestCustomerAsset param) {
        ApiRequestCustomerAssetData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("crdFundId", data.getCrdFundId());
        jsonObject.put("optFundId", data.getOptFundId());
        jsonObject.put("ordFundId", data.getOrdFundId());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerOverallBigdataAsset(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseGetCustomerOverallAssetData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseGetCustomerOverallAssetData.class);
        return ResponseEntity.ok(new ApiResponseGetCustomerOverallAsset().data(resultEntity));
    }

    @Override
    public ResponseEntity<ApiResponseCustomerAccount> getCustomerTransitDetailBigdata(ApiRequestCustomerAccount param) {
        ApiRequestCustomerAccountData data = param.getData();
        ServiceHelper.checkDataNotNull(data, "data");
        String customerId = data.getCustomerId();
        ServiceHelper.checkDataNotNull(customerId, "data.customerId");
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("customerId", customerId);
        jsonObject.put("fundId", data.getFundId());
        ResponseEntity<ApiResponseFsdp> fsdpResponseEntity = innerOverAllBigDataService.getCustomerTransitDetailBigdata(new ApiRequesFsdp().data(new ApiRequesFsdpData().params(jsonObject.toJSONString())));
        ApiResponseCustomerAccountData resultEntity = JsonUtils.getResultEntity(fsdpResponseEntity, ApiResponseCustomerAccountData.class);
        return ResponseEntity.ok(new ApiResponseCustomerAccount().data(resultEntity));
    }
}
